﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Control_IndividualInfonew : System.Web.UI.UserControl
{
    private Srvtools.WebDataSet WMasterIndividual;
    private int _eid;
    private bool _action;
    private bool _readonly;
    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {

            HidAction.Value = Action.ToString();

            InitializeComponent(true);
            WMasterIndividual.SetWhere("Employees.EmployeeId=" + Eid);
            MasterIndividual.DataSource = WMasterIndividual;

            wnSelf.Visible = !ReadOnly;

        }

        Bind();
    }





    private void Bind()
    {
        if (Convert.ToBoolean(HidAction.Value))
        {
            wnSelf.NavControls.Remove(wnSelf.NavControls["Update"]);
        }
        else
        {
            wnSelf.NavControls.Remove(wnSelf.NavControls["Add"]);

        }



    }

    public int Eid
    {
        get
        {
            return _eid;
        }
        set
        {
            _eid = value;
        }

    }

    public bool Action
    {
        get
        {
            return _action;
        }
        set
        {
            _action = value;
        }
    }


    public bool ReadOnly
    {
        get
        {
            return _readonly;
        }
        set
        {
            _readonly = value;
        }
    }



    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Control_IndividualInfonew));
        this.WMasterIndividual = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMasterIndividual)).BeginInit();
        // 
        // WMasterIndividual
        // 
        this.WMasterIndividual.Active = action;
        this.WMasterIndividual.AlwaysClose = true;
        this.WMasterIndividual.DataCompressed = false;
        this.WMasterIndividual.DeleteIncomplete = true;
        this.WMasterIndividual.Guid = null;
        this.WMasterIndividual.LastKeyValues = null;
        this.WMasterIndividual.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMasterIndividual.PacketRecords = 100;
        this.WMasterIndividual.Position = -1;
        this.WMasterIndividual.RemoteName = "SEmployees.EmployeesIndividual";
        this.WMasterIndividual.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMasterIndividual)).EndInit();

    }


    protected void wnSelf_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "cmdFirst" || e.CommandName == "cmdPrevious"
            || e.CommandName == "cmdNext" || e.CommandName == "cmdLast")
        {
            MasterIndividual.ExecuteSelect(FVIndividual);
            DataBind();
        }
        else if (e.CommandName == "cmdAdd")
        {
            //MasterIndividual.ExecuteAdd(FVIndividual);
            DataBind();
        }
        else if (e.CommandName == "cmdDelete")
        {
            MasterIndividual.ExecuteSelect(FVIndividual);
            DataBind();
        }
        else if (e.CommandName == "cmdApply" && FVIndividual.AllValidateSucess)
        {
            MasterIndividual.ExecuteSelect(FVIndividual);
            DataBind();

        }

    }


    private void BindRole(object Uid, object Email, FormViewMode fv)
    {
        Control_UserGroup UG = FVIndividual.FindControl("UserGroup") as Control_UserGroup;

        if (Uid != null)
        {
            if (fv == FormViewMode.ReadOnly)
            {
                UG.GetGroup(true, Uid.ToString());
            }
            else
            {
                UG.GetGroup(false, Uid.ToString());
            }

            hidEmail.Value = Email == null ? "" : Email.ToString();
            hidGClass2.Value = UG.NowChoose;
        }
        else
        {
            if (fv == FormViewMode.Insert)
            {
                UG.GetGroup(false, "");
            }
            else
            {
                if (hidGClass2Name.Value != "")
                {
                    UG.LikeReadOnlyBind(hidGClass2Name.Value);
                }
                else
                {
                    UG.GetGroup(true, "");
                }
            }
        }
    }


    protected void FVIndividual_DataBound(object sender, EventArgs e)
    {
        //hidEmail
        try
        {
            if (Convert.ToBoolean(HidAction.Value))
            {
                BindRole(null, null, FVIndividual.CurrentMode);
            }
            else
            {

                string EmpId = MasterIndividual.CurrentRow["EmployeeID"].ToString();

                int Length = EmpId.Length;

                string Uid = EmpId;

                for (int i = Length; i < 4; i++)
                {
                    Uid = "0" + Uid;
                }

                BindRole(Uid, (object)MasterIndividual.CurrentRow["EMAIL"], FVIndividual.CurrentMode);
            }

            System.Web.UI.HtmlControls.HtmlTableCell tns = FVIndividual.FindControl("tns") as System.Web.UI.HtmlControls.HtmlTableCell;
            System.Web.UI.HtmlControls.HtmlTableCell tus = FVIndividual.FindControl("tus") as System.Web.UI.HtmlControls.HtmlTableCell;
            CheckBox chb = FVIndividual.FindControl("CanLogin") as CheckBox;
            chb.Attributes.Add("onclick", "ShowRole(this)");
            if ((bool)MasterIndividual.CurrentRow["CanLogin"])
            {
                tns.Attributes.Add("style", "visibility:visible");
                tus.Attributes.Add("style", "visibility:visible");

            }
            else
            {
                tns.Attributes.Add("style", "visibility:hidden");
                tus.Attributes.Add("style", "visibility:hidden");
            }
        }
        catch
        {
        }
    }

    protected void MasterIndividual_Adding(object sender, Srvtools.WebDataSourceAddingEventArgs e)
    {
        try
        {
           
            Control_UserGroup UG = FVIndividual.FindControl("UserGroup") as Control_UserGroup;
            e.AddingValues["GroupName"] = UG.NowChoose;

            hidGClass2.Value = UG.NowChoose;
            hidGClass2Name.Value = UG.NowChooseName;
        }
        catch
        {
        }
    }

    protected void MasterIndividual_Updating(object sender, Srvtools.WebDataSourceUpdatingEventArgs e)
    {
        Control_UserGroup UG = FVIndividual.FindControl("UserGroup") as Control_UserGroup;
        try
        {


            if (UG.NowChoose != hidGClass2.Value)
            {
                e.UpdatingValues["GroupName"] = UG.NowChoose;

                if (MasterIndividual.CurrentRow["Email"].ToString() != "")
                {
                    Srvtools.WebMail wm = new Srvtools.WebMail();
                    wm.From = System.Configuration.ConfigurationSettings.AppSettings["SendEmailFrom"].ToString();
                    wm.Password = System.Configuration.ConfigurationSettings.AppSettings["SendEmailPassword"].ToString();
                    wm.Host = System.Configuration.ConfigurationSettings.AppSettings["SendEmailHost"].ToString();
                    wm.Port = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["SendEmailPort"]);

                    wm.Subject = "OA-您的类别已更改";
                    wm.To = MasterIndividual.CurrentRow["Email"].ToString();

                    wm.IsBodyHtml = true;
                    wm.Body = "您的类别已更改为：<br><br><br><b>" + UG.NowChooseName + "</b>";
                    wm.Send(false);
                }
            }
            else
            {
                e.UpdatingValues["GroupName"] = "";
            }



        }
        catch
        {
        }
        finally
        {

        }
    }

}
